/*
 * QuickSend.java
 * Created on 2010-nov-16, 14:16:37
 */

package simplepushclient;


import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
/**
 * @author Filip
 */
public class HistorySend extends javax.swing.JFrame {
    private  PusherTray p;
    private DefaultListModel listmodel;
    /** Creates new form QuickSend */
    public HistorySend(PusherTray parent) {
        this.p = parent;
        this.listmodel = new DefaultListModel();
        ArrayList<String> l = p.getHistory().getList();
        for (int i = l.size()-1; i > -1; i--) {
            listmodel.addElement(l.get(i));
        }
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("HistoryPush");
        setIconImage(p.getImage());
        setMinimumSize(new java.awt.Dimension(230, 150));

        jList1.setModel(listmodel);
        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jList1KeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        jButton1.setText("Send");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
                    .addComponent(jButton1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        p.send((String)jList1.getSelectedValue());
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jList1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jList1KeyPressed
        if(evt.getKeyCode()==KeyEvent.VK_ENTER){
            p.send((String)jList1.getSelectedValue());
       }else if(evt.getKeyCode() == KeyEvent.VK_DELETE){
            if(evt.getModifiers()==KeyEvent.VK_SHIFT)
                p.getHistory().getList().remove((String)jList1.getSelectedValue());
            removeSelected();
       }
    }//GEN-LAST:event_jList1KeyPressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
    private void removeSelected(){
       int i = jList1.getSelectedIndex();
       listmodel.remove(i);
       jList1.setSelectedIndex(i%listmodel.size());
    }
}
